<!--
  *  员工 表单 弹窗
  * 
  * @Author:    1024创新实验室-主任：卓大 
  * @Date:      2022-08-08 20:46:18
  * @Wechat:    zhuda1024 
  * @Email:     lab1024@163.com 
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012 
-->
<template>
  <a-drawer
    :title="!iaddconfig? '编辑修改规则' : '新增规则配置并应用'"
    :width="600"
    :visible="visible"
    :body-style="{ paddingBottom: '80px' }"
    @close="onClose"
    destroyOnClose
  >
    <a-form ref="formRef" :model="form" :rules="rules" layout="vertical">
	  <a-form-item label="驻点医馆名称" name="si_name">
	  <a-input v-model:value.trim="form.si_name" disabled placeholder="请输入驻点医馆名称" />
	  </a-form-item>
	  <a-form-item label="医生名称" name="name">
	    <a-input v-model:value.trim="form.name" disabled placeholder="请输入医馆名称" />
	  </a-form-item>
	  <a-form-item label="医生头像" name="icon">
	  	<a-image :src="form.icon" disabled style="max-width: 150px; max-height: 150px;" />
	  </a-form-item>
	  <a-form-item label="坐诊方式" name="worktab">
	    <a-input v-model:value.trim="form.worktab" disabled placeholder="请输入医生坐诊方式" />
	  </a-form-item>
	  <a-form-item label="坐诊规则名称" name="rulesname">
	    <a-input v-model:value.trim="form.rulesname" placeholder="请输入医生坐诊规则名称" />
	  </a-form-item>
<!--		<a-form-item label="医馆是否开启" name="frameFlag">-->
<!--			<a-switch v-model:checked="form.is_opened" checked-children="启用" un-checked-children="禁用" />-->
<!--		</a-form-item>-->
<!--			<a-form-item label="医馆状态" name="frameFlag">-->
<!--				<a-switch v-model:checked="form.status" checked-children="正常" un-checked-children="禁用" />-->
<!--			</a-form-item>-->
	  <a-form-item label="出诊周期" name="workcycle">
		<p class="hint">表示出诊时间一般是：周几</p>
	    <a-select mode="multiple" v-model:value="form.workcycle" optionFilterProp="title" placeholder="请选择出诊周期">
	      <a-select-option value="周一">周一</a-select-option>
	      <a-select-option value="周二">周二</a-select-option>
	  		  <a-select-option value="周三">周三</a-select-option>
	  		  <a-select-option value="周四">周四</a-select-option>
	  		  <a-select-option value="周五">周五</a-select-option>
	  		  <a-select-option value="周六">周六</a-select-option>
	  		  <a-select-option value="周日">周日</a-select-option>
	    </a-select>
	  </a-form-item>

	  <a-form-item label="间隔时段(单位/分钟)" name="interval_time">
		<p class="hint">表示每隔多少分钟拍一次班</p>
		<a-input v-model:value.trim="form.interval_time" placeholder="请输入医生间隔时段" />
	  </a-form-item>
	  <a-form-item label="早上排班起始时间" name="interval_am_sart">
		<p class="hint">表示早上开始排班的起始时间</p>
		<a-input v-model:value.trim="form.interval_am_sart" placeholder="请输入医生早上排班起始时间" />
	  </a-form-item>
	  <a-form-item label="早上排班截止时间" name="interval_am_end">
		<p class="hint">表示早上开始排班的截止时间</p>
	    <a-input v-model:value.trim="form.interval_am_end" placeholder="请输入医生早上排班起始时间" />
	  </a-form-item>
	  <a-form-item label="下午排班起始时间" name="interval_pm_sart">
		<p class="hint">表示下午开始排班的起始时间</p>
	  	<a-input v-model:value.trim="form.interval_pm_sart" placeholder="请输入医生下午排班起始时间" />
	  </a-form-item>
	  <a-form-item label="下午排班截止时间" name="interval_pm_end">
		<p class="hint">表示下午开始排班的截止时间</p>
	  	<a-input v-model:value.trim="form.interval_pm_end" placeholder="请输入医生下午排班起始时间" />
	  </a-form-item>
	  <a-form-item label="时段号源数" name="interval_scheduling_stock">
	  	<p class="hint">表示每个时段放出的号源数量</p>
	    <a-input v-model:value.trim="form.interval_scheduling_stock" placeholder="请输入显示时段号源数" />
	  </a-form-item>
	  <a-form-item label="出诊天数" name="interval_days">
		<p class="hint">表示显示多少天的就诊天数，控制显示最近多少天的预约</p>
	    <a-input v-model:value.trim="form.interval_days" placeholder="请输入显示多少天出诊天数" />
	  </a-form-item>
	  	 
	  
	 <!-- <a-form-item label="坐标名称" name="location_name">
	    <a-input v-model:value.trim="form.location_name" placeholder="请输入坐标名称" />
	  </a-form-item>
	  <a-form-item label="坐标地址" name="location">
	    <a-input v-model:value.trim="form.location" placeholder="请输入坐标地址" />
		<p class="hint">使用逗号分割</p>
	  </a-form-item> -->
    <!--  <a-form-item label="状态" name="status">
        <a-select v-model:value="form.status" placeholder="请选择状态">
          <a-select-option :value="0">启用</a-select-option>
          <a-select-option :value="1">禁用</a-select-option>
        </a-select>
      </a-form-item> -->
     
    </a-form>
    <div class="footer">
      <a-button style="margin-right: 8px" @click="onClose">取消</a-button>
      <a-button type="primary" style="margin-right: 8px" @click="onSubmit(false)">{{!iaddconfig?"修改并保存":"新增并应用"}}</a-button>
      <a-button v-if="!form.id" type="primary" @click="onSubmit(true)">保存并继续添加</a-button>
    </div>
  </a-drawer>
</template>
<script setup lang="ts">
  import { message } from 'ant-design-vue';
  import _ from 'lodash';
  import { nextTick, reactive, ref } from 'vue';
  import { roleApi } from '/@/api/system/role/role-api';
  import { businessesApi } from '/@/api/system/infirmary/businesses-api';
  import DepartmentTreeSelect from '/@/components/system/department-tree-select/index.vue';
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
  import { GENDER_ENUM } from '/@/constants/common-const';
  import { regular } from '/@/constants/regular-const';
  import { SmartLoading } from '/@/components/framework/smart-loading';
import { smartSentry } from '/@/lib/smart-sentry';
  // ----------------------- 以下是字段定义 emits props ---------------------
  const departmentTreeSelect = ref();
  // emit
  const emit = defineEmits(['refresh', 'show-account']);

  // ----------------------- 显示/隐藏 ---------------------

  const visible = ref(false); // 是否展示抽屉
	const iaddconfig = ref(false); // 是否展示抽屉
  // 隐藏
  function onClose() {
    reset();
    visible.value = false;
  }
  // 显示
  async function showDrawer(StationRecord,DoctorRecord,isadd) {
	// console.log("关闭了as显示",rowData,!_.isEmpty(rowData))
	 //是否是修改或新增
		iaddconfig.value = isadd;
		formDefault.si_number	=	StationRecord.si_number;
		formDefault.is_opened	=	StationRecord.is_opened===1?true:false;
		formDefault.status	=	StationRecord.status===1?true:false;




		if (DoctorRecord && !_.isEmpty(DoctorRecord)) {
			Object.assign(form, DoctorRecord);
			Object.assign(form, formDefault);
		}else{
			Object.assign(form, {});
		}
		if (!isadd) {
			// 执行联网获取对应的规则信息
			ajaxQuery(StationRecord,DoctorRecord)
		}else {
			// 给医生新增新的配置规则属性
			// 清除情况的属性值
			// formRef.value.resetFields();
			Object.assign(form, formDefault);
			visible.value = true;

		}
  }


  async function ajaxQuery(StationRecord,DoctorRecord) {
    try {
	   // console.log("输AAAAAAAAAAAA出",Appid,queryForm.appid)
	   // 查询医生医馆对应的排班的规则
			var queryForm = {
				'appid':DoctorRecord.appid,
				'si_number':StationRecord.si_number,
				'dc_number':DoctorRecord.dc_number,
			}
				let responseModel = await businessesApi.queryBusinessesDoctorStationRule(queryForm);
			// 获取规则成功才显示具体规则信息
			 const list = responseModel.data;
			 Object.assign(form, list);
			visible.value = true;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
		 
    }
  }

  // ----------------------- 表单显示 ---------------------
  const formRef = ref(); // 组件ref
  // 添加的时候相关表单信息的是否显示的关键点----------清除关键点
  const formDefault = {
    id: undefined,
	  appid:undefined,
	  name:undefined,
		si_name:undefined,
		icon:undefined,
		worktab:undefined,
		si_number:undefined,
		rulesname:undefined,
		workcycle:undefined,
		interval_am_sart:undefined,
		interval_am_end:undefined,
		interval_pm_sart:undefined,
		interval_pm_end:undefined,
		interval_scheduling_stock:undefined,
		interval_days:undefined,
		status:undefined,
		interval_time:undefined,
		is_opened:undefined,
  };

  let form = reactive(_.cloneDeep(formDefault));
  function reset() {
    Object.assign(form, formDefault);
    formRef.value.resetFields();
  }

  // ----------------------- 表单提交 ---------------------
  // 表单规则
  const rules = {
	rulesname: [
      { required: true, message: '医生坐诊规则名称不能为空' },
    ],
    worktab: [
      { required: true, message: '坐诊方式不能为空' },
    ],
	workcycle: [
	  { required: true, message: '坐诊周期不能为空' },
	],
	interval_scheduling_stock: [
	  { required: true, message: '时段号源总数不能为空' },
	],
	interval_days: [
	  { required: true, message: '显示排班天数不能为空' },
	],
	interval_time: [
	  { required: true, message: '间隔时段不能为空' },
	],
	interval_am_sart: [
	  { required: true, message: '早上排班起始时间不能为空' },
	],
	interval_am_end: [
	  { required: true, message: '早上排班截止时间不能为空' },
	],
	interval_pm_sart: [
	  { required: true, message: '下午排班起始时间不能为空' },
	],
	interval_pm_end: [
	  { required: true, message: '下午排班截止时间不能为空' },
	],
    name: [
      { required: true, message: '医馆名称不能为空' },
    ],
	tel: [
	  { required: true, message: '联系号码不能为空' },
	  { pattern: regular.tel, message: '请输入正确的手机号码', trigger: 'blur' },
	],
    status: [{ required: true, message: '状态不能为空' }],
  };

  // 校验表单
  function validateForm(formRef) {
    return new Promise((resolve) => {
      formRef
        .validate()
        .then(() => {
          resolve(true);
        })
        .catch(() => {
          resolve(false);
        });
    });
  }

  // 提交数据
  async function onSubmit(keepAdding) {
    let validateFormRes = await validateForm(formRef.value);
    if (!validateFormRes) {
      message.error('参数验证错误，请仔细填写表单数据!');
      return;
    }
    SmartLoading.show();
    // 更新规则
		if(!iaddconfig.value){
			await updateBusinessesDoctorStationRule(keepAdding);
		}else{
    	//给医生添加配置并应用新的规则配置
			await addNewBusinessesDoctorStationRule(keepAdding)
		}
  }

	async function addNewBusinessesDoctorStationRule(keepAdding) {
		try {
			let result = await businessesApi.addNewBusinessesDoctorStationRule(form);
			const ok = result.ok;
			message.success(result.msg);
			if (keepAdding) {
				reset();
			} else {
				onClose();
			}
			// emit('refresh');
			emit('reloadList');
		} catch (error) {
			// smartSentry.captureError(error);
		} finally {
			SmartLoading.hide();
		}
	}

  async function updateBusinessesDoctorStationRule(keepAdding) {
    try {
      let result = await businessesApi.updateBusinessesDoctorStationRule(form);
	  const ok = result.ok;
	  message.success(result.msg);
      if (keepAdding) {
        reset();
      } else {
        onClose();
      }
      // emit('refresh');
			emit('reloadList');
    } catch (error) {
      // smartSentry.captureError(error);
    } finally {
      SmartLoading.hide();
    }
  }

  // ----------------------- 以下是暴露的方法内容 ----------------------------
  defineExpose({
    showDrawer,
  });
</script>
<style scoped lang="less">
  .footer {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e9e9e9;
    padding: 10px 16px;
    background: #fff;
    text-align: right;
    z-index: 1;
  }
  .hint {
    margin-top: 5px;
    color: #bfbfbf;
  }
</style>
